package eg;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;





//TODO 规范问题|类名首字母大写
//public class money {
public class Money {


    private static String allmoney="0";
    private static String filePath;
    private static boolean append;


    public static void saveMoneyToText( ArrayList<Moneydemo> array){
        try {

            append=true;
            FileWriter fw =null;
            fw = new FileWriter("D://Money.txt",append);

            BufferedWriter bw = new BufferedWriter(fw);


            for (Moneydemo b:array){
                for (int i = 0; i < array.size(); i++) {
                    b = array.get(i);

                    bw.write((i+1)+"\t" +b.getChangemoney() + "\t" +"交易方："+ b.getName() + "\t" +"余额："+ b.getAllmoney() + "\t" + sdf.format(b.getCreateDate()));
                }
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
  /* public static void readTxtFile(ArrayList<String> array ){

       try {
        String thisLine=null;
       File file=new File("D://Money.txt");

            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("D://Money.txt")));
            while((thisLine=br.readLine())!=null){
                array.add(thisLine);
            }
            br.close();
        } catch (IOException e) {
           throw new RuntimeException(e);
       }


   }
*/









    public static void main(String[] args) throws IOException {

        ArrayList<Moneydemo> array = new ArrayList<Moneydemo>();
        String filePath = "D://Money.txt";

        while (true) {
            System.out.println("---------------------------------零钱通菜单---------------------------------");
            System.out.println("1 零钱通明细");
            System.out.println("2 收益入账");
            System.out.println("3 消费");
            System.out.println("4 删除信息");
            System.out.println("5 修改信息");
            System.out.println("6 退     出");
            System.out.println("请选择（1-6）： ");

            Scanner bb = new Scanner(System.in);
            String a = bb.nextLine();

            switch (a) {
                case "1":
                    c(array);//明细
                    break;

                case "2":
                    income(array);
                    break;
                case "3":
                    outmoney(array);
                    break;
                case "4":
                    xxhistory(array);
                    break;
                case "5":
                    modification(array);
                    break;
                case "6":
                    System.out.println("感谢你的使用");
                    System.exit(0);

                default:
                    System.out.println("你的输入有误");


            }
        }




    }

    public static void income(ArrayList<Moneydemo> array) {
        Scanner sc = new Scanner(System.in);

        //Date date = new Date();
        System.out.println("------------------------------收益入账------------------------------------------------");
        System.out.println("请输入金额");
        String changemoney = sc.nextLine();
        Double a = Double.parseDouble(changemoney);


        Double n = Double.parseDouble(allmoney);
        n = n + a;
        allmoney = String.valueOf(n);
        System.out.println("请输入转账方");
        String name = sc.nextLine();


        //TODO 这里可以直接存Date对象的，存Date对象只需要一个字段
//        String day1 = String.format("%tF", date);
//        String day2 = String.format("%tT", date);


        Moneydemo b = new Moneydemo();
        b.setChangemoney(changemoney);
        b.setCreateDate(new Date());
        b.setAllmoney(allmoney);
        b.setName(name);

        //b.setDate2(day2);

        //TODO 体验问题|最新时间的应该排在上面，所以这里可以在最前面增加
        //array.add(b);
        array.add(0, b);

        //TODO 收益入账以后,可以直接显示最新的列表
//        System.out.println(b.getChangemoney() + "\t" + b.getDate1() + "\t" + b.getDate2());
        c(array);
        saveMoneyToText(array);

    }



    public static void outmoney(ArrayList<Moneydemo> array) {
        Scanner sc = new Scanner(System.in);
        //Date date = new Date();
        System.out.println("------------------------------消费------------------------------------------------");
        System.out.println("请输入收款单位名");
        String name = sc.nextLine();
        System.out.println("请输入金额");
        String changemoney = sc.nextLine();
        Double a = Double.parseDouble(changemoney);

        Double n = Double.parseDouble(allmoney);
        n = n + a;
        allmoney = String.valueOf(n);

        Moneydemo b = new Moneydemo();
        b.setChangemoney(changemoney);
        b.setCreateDate(new Date());
        b.setName(name);
        b.setAllmoney(allmoney);

        array.add(0, b);
        c(array);
        saveMoneyToText(array);

    }



    //使用这个对象可以格式化日期
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void c(ArrayList<Moneydemo> array) {
        try {
            String thisLine=null;
            File file=new File("D://Money.txt");

            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("D://Money.txt"),"GBK"));
            while((thisLine=br.readLine())!=null){
                    //TODO 提交的代码要保证不会报错
                    //     thisLine是一个字符串，不能添加到ArrayList<Moneydemo> array里面
                    //array.add(thisLine);

            }

            br.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        for (int i = 0; i < array.size(); i++) {
            Moneydemo b = array.get(i);
            //TODO 删除功能实现,先把数组下标打印出来
            //  因为下标是从零开始的，所以这里加1，是为了显示效果，从0.1.2.3.4.... 的显示效果改为1.2.3.4...
//            System.out.println(b.getChangemoney() + "\t" + b.getDate1() + "\t" + b.getDate2());
            System.out.println((i+1) + "\t"  + b.getChangemoney() + "\t"  +"交易方:"+ b.getName()+"\t"  + "余额:"+b.getAllmoney()+  "\t"+sdf.format(b.getCreateDate()));
        }





    }

    public static void modification(ArrayList<Moneydemo> array) {
        Scanner sc = new Scanner(System.in);
        c(array);
        System.out.println("请输入需要修改的序号");
        try {
            int i = sc.nextInt();


            i--;

            if(array.size()==0){
                System.out.println("列表为空无法修改");
                return;
            }
            if(i<=array.size()+1){
                Scanner kkk = new Scanner(System.in);

                System.out.println("请输入新的收支明细金额");
                String changemoney=kkk.nextLine();
                Double a = Double.parseDouble(changemoney);
                Double n = Double.parseDouble(allmoney);
                n = n + a;
                allmoney = String.valueOf(n);

                System.out.println("请输入新的收支明细单位");
                String name=kkk.nextLine();

                Moneydemo b=new Moneydemo();
                b.setChangemoney(changemoney);
                b.setName(name);
                b.setCreateDate(new Date());
                b.setAllmoney(allmoney);

                for(int j=0;j<array.size()+1;j++){
                    Moneydemo moneydemo = array.get(j);
                    if(j==i){
                        array.set(j,b);
                        break;
                    }
                }

            }else{
                System.out.println("请输入正确的序号");
                modification(array);
            }
            System.out.println("修改成功！");
        }catch (Exception e){
            //TODO 输入非法字符时需要有对应的提示
            System.out.println("请输入正确的序号");
            modification(array);
        }


    }
    public static void xxhistory(ArrayList<Moneydemo> array){
        Scanner sc=new Scanner(System.in);
        c(array);
        System.out.println("请输入你要删除的序号：");
        try {
            int i = sc.nextInt();
            i--;
            if(array.size()==0){
                System.out.println("列表为空不能删除");
                return;
            }
            if(i<=array.size()+1){
                array.remove(i);
            }else{
                System.out.println("请输入正确的序号");
                xxhistory(array);
            }
            System.out.println("删除成功！");
        }catch (Exception e){
            //TODO 输入非法字符时需要有对应的提示
            System.out.println("请输入正确的序号");
            xxhistory(array);
        }

    }




}
